import { Component, OnInit } from '@angular/core';
import { MenuItem, MenuItemType } from '../../common/menu/menu.component';
import { AdminService } from '../admin.service';
import { Subject, of } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap, map } from 'rxjs/operators';



@Component({
  selector: 'app-vip-course',
  templateUrl: './vip-course.component.html',
  styleUrls: ['./vip-course.component.less']
})
export class VipCourseComponent implements OnInit {

  isShow = false;

  menuData: MenuItem[] = [
    { label: 'html课程', linkUrl: 'http://baidu.com', type: MenuItemType.Link },
    { label: '用户中心', routeUrl: '/admin/vip-course', type: MenuItemType.Route },
    {
      label: '退出登录', callback: () => {
        alert(1);
      }, type: MenuItemType.Callback
    }
  ];


  // 搜索结果
  seachData: MenuItem[] = [];

  subject: Subject<string> = new Subject<string>();

  constructor(private as: AdminService) {
    this.subject.pipe(
      debounceTime(300),
      distinctUntilChanged(),
      switchMap(keyword => {
        if (!keyword) {
          return of(null);
        }

        return this.as.search(keyword);
      })

    ).subscribe(result => {
      if (!result) {
        this.seachData = [];
        return;
      }
      this.seachData = result.data.map(item => {
        return {
          label: item,
          linkUrl: 'http://qq.com',
          type: 'link'
        }
      });
    });
  }

  ngOnInit() {
  }

  searchHandle(val: string) {
    this.subject.next(val);
  }

}
